import api from '../../api';
import type { Permission } from '../../../types/rbac';
import type { PageResponse } from '../../api';

// 权限查询参数
export interface PermissionListParams {
  page?: number;
  size?: number;
  name?: string;
  code?: string;
  type?: string; // "menu"-菜单，"button"-按钮，"api"-API
  parentId?: string;
  status?: number; // 1-启用，0-禁用
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
}

export interface CreatePermissionRequest {
  name: string;
  code: string;
  type: string; // "menu"-菜单，"button"-按钮，"api"-API
  category: string; // 权限分类，如 'system', 'user', 'role' 等
  parentId?: string;
  path?: string;
  method?: string;
  description?: string;
  sort?: number;
  icon?: string;
  component?: string;
  redirect?: string;
  hidden?: boolean;
  alwaysShow?: boolean;
  isBuiltin?: boolean;
  priority?: number;
  status?: number; // 1-启用，0-禁用
}

export interface UpdatePermissionRequest {
  name?: string;
  code?: string;
  type?: string; // "menu"-菜单，"button"-按钮，"api"-API
  category?: string; // 权限分类，如 'system', 'user', 'role' 等
  parentId?: string;
  path?: string;
  method?: string;
  description?: string;
  sort?: number;
  icon?: string;
  component?: string;
  redirect?: string;
  hidden?: boolean;
  alwaysShow?: boolean;
  isBuiltin?: boolean;
  priority?: number;
  status?: number; // 1-启用，0-禁用
}

export interface PermissionListResponse {
  records: Permission[];
  totalElements: number;
  totalPages: number;
  size: number;
  number: number;
  first: boolean;
  last: boolean;
}

export interface PermissionTree extends Permission {
  children?: PermissionTree[];
}

export interface MovePermissionRequest {
  permissionId: string;
  targetParentId?: string;
  targetSort: number;
}

// 权限管理API服务 - 通过网关调用v3-system服务
export const permissionApi = {
  // 分页查询权限列表
  getPermissions: (params?: PermissionListParams): Promise<PermissionListResponse> => {
    return api.post('/system/api/v3/sys/permissions/page', {
      current: params?.page || 1,
      size: params?.size || 10,
      name: params?.name,
      code: params?.code,
      type: params?.type,
      status: params?.status,
    });
  },

  // 获取所有权限（不分页）
  getAllPermissions: (): Promise<Permission[]> => {
    return api.get('/system/api/v3/sys/permissions/all');
  },

  // 获取权限树
  getPermissionTree: (params?: {
    type?: string;
    status?: string;
  }): Promise<PermissionTree[]> => {
    return api.get('/system/api/v3/sys/permissions/tree', { params });
  },

  // 获取菜单权限树（用于路由生成）
  getMenuTree: (): Promise<PermissionTree[]> => {
    return api.get('/system/api/v3/sys/permissions/menu-tree');
  },

  // 获取权限详情
  getPermission: (id: string): Promise<Permission> => {
    return api.get(`/system/api/v3/sys/permissions/get/${id}`);
  },

  // 创建权限
  createPermission: (data: CreatePermissionRequest): Promise<Permission> => {
    return api.post('/system/api/v3/sys/permissions/add', data);
  },

  // 更新权限
  updatePermission: (id: string, data: UpdatePermissionRequest): Promise<Permission> => {
    return api.put('/system/api/v3/sys/permissions/update', { ...data, id });
  },

  // 删除权限
  deletePermission: (id: string): Promise<void> => {
    return api.delete(`/system/api/v3/sys/permissions/delete/${id}`);
  },

  // 批量删除权限
  batchDeletePermissions: (ids: string[]): Promise<void> => {
    return api.delete('/system/api/v3/sys/permissions/batch', { data: ids });
  },

  // 移动权限（调整父级和排序）
  movePermission: (data: MovePermissionRequest): Promise<void> => {
    return api.put('/system/api/v3/sys/permissions/move', data);
  },

  // 批量更新权限排序
  batchUpdateSort: (data: Array<{
    id: string;
    sort: number;
  }>): Promise<void> => {
    return api.put('/system/api/v3/sys/permissions/batch-sort', data);
  },

  // 检查权限编码是否存在
  checkPermissionCode: (code: string, excludeId?: string): Promise<{ exists: boolean }> => {
    return api.get('/system/api/v3/sys/permissions/check-code', {
      params: { code, excludeId }
    });
  },

  // 获取子权限列表
  getChildPermissions: (parentId: string): Promise<Permission[]> => {
    return api.get(`/system/api/v3/sys/permissions/${parentId}/children`);
  },
};